package com.hong610.net.http.response;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.hong610.net.config.Config;
import com.hong610.net.enums.NetHandle;

/**
 * 数据处理
 * Created by Hong on 2016/12/31.
 */
public class HandleResponse {
	
	private static String charset = Config.CHARSET;

	/**
	 * 处理
	 * @param response HttpResponse
	 * @param handle handle
	 * @param charset charset
	 * @return String
	 */
	public static String handle(HttpResponse response, NetHandle handle, String charset) {
		return handle(response, handle, charset, null);
	}

	/**
	 * 处理
	 * @param response response
	 * @param handle handle
	 * @param charset charset
	 * @param savePath savePath
	 * @return String
	 */
	public static String handle(HttpResponse response, NetHandle handle, String charset, String savePath) {
		HandleResponse.charset = charset;
		try{
			if(handle == NetHandle.NORMAL){
				return handleByNormal(response);
			}else if(handle == NetHandle.STREAM){
				return handleByStream(response);
			}else if(handle == NetHandle.DOWNLOAD){
				return String.valueOf(handleByDownload(response, savePath));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 下载方式
	 * @param response response
	 * @param savePath 保存路径
	 * @return boolean
	 */
	private static boolean handleByDownload(HttpResponse response, String savePath) throws Exception {
		HttpEntity httpEntity = response.getEntity();
		if(httpEntity == null)
			return false;
		InputStream inputStream = httpEntity.getContent();
		if(inputStream == null)
			return false;
		if(response.getStatusLine().getStatusCode() != 200){
			return false;
		}
		File file =new File(savePath);
	    try{
	        FileOutputStream fout =new FileOutputStream(file);
	        int l = -1;
	        byte[] tmp =new byte[1024];
	        while((l = inputStream.read(tmp)) != -1) {
	            fout.write(tmp,0, l);
	            // 注意这里如果用OutputStream.write(buff)的话，图片会失真，大家可以试试
	        }
	        fout.flush();
	        fout.close();
	    }catch(Exception e){
	    	return false;
	    }
	    finally{
	        // 关闭流。
	    	inputStream.close();
	    }
		return true;
	}

	/**
	 * 流方式处理
	 * @param response response
	 * @return String
	 * @throws Exception
	 */
	private static String handleByStream(HttpResponse response) throws Exception {
		HttpEntity httpEntity = response.getEntity();
		if(httpEntity == null)
			return null;
		
		InputStream inputStream = httpEntity.getContent();
		if(inputStream == null)
			return null;
		
		StringBuffer postResult = new StringBuffer();
		String readLine = null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset));
		while ((readLine = reader.readLine()) != null) {
			postResult.append(readLine);
		}
		return postResult.toString();
	}

	/**
	 * 正常处理  返回字符串
	 * @param response response
	 * @return String
	 * @throws Exception
	 */
	private static String handleByNormal(HttpResponse response) throws Exception {
		if (response == null) 
			return null;
		
		HttpEntity resEntity = response.getEntity();
		if (resEntity == null)
			return null;
		
		return EntityUtils.toString(resEntity, charset);
	}
}
